package com.alan.swagger2.bean;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ResultBean<T> implements Serializable {
    private static final long  serialVersionUID = 3857819309241844666L;
    public static final String SUCCESS          = "0";
    public static final String FAIL             = "1";
    @Getter
    @Setter
    private String message          = "success";
    @Getter
    @Setter
    private String code             = SUCCESS;
    @Getter
    @Setter
    private T                  result;

    public ResultBean() {
        super();
    }

    public ResultBean(T result) {
        super();
        this.result = result;
    }

    public ResultBean(String code, String message) {
        super();
        this.code = code;
        this.message = message;
    }

    public ResultBean(Throwable e) {
        super();
        this.message = e.toString();
        this.code = FAIL;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return this.code.equals(SUCCESS);
    }

    public ResultBean<T> setErrorMsg(String msg) {
        this.message = msg;
        this.code = FAIL;

        return this;
    }

    public ResultBean<T> setError(String code, String msg) {
        this.message = msg;
        this.code = code;
        return this;
    }

    public ResultBean<T> setError(String msg) {
        this.message = msg;
        this.code = FAIL;
        return this;
    }


}
